package com.hrm.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.hrm.dao.EmpVacationMapper;
import com.hrm.dao.EmployeeMapper;
import com.hrm.dao.SysEventMapper;
import com.hrm.dao.VacationMapper;
import com.hrm.dto.AuditVacationDTO;
import com.hrm.entity.EmpVacation;
import com.hrm.entity.Employee;
import com.hrm.entity.SysEvent;
import com.hrm.entity.Vacation;
import com.hrm.service.VacationService;
import com.hrm.utils.SysEventDescription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: 任旭旺
 * @Date: 2020/5/31 22:35
 * @Description: 申请假期 批准假期 假期设置等一系列功能
 */
@Service
public class VacationServiceImpl implements VacationService {
    @Autowired
    VacationMapper vacationMapper;
    @Autowired
    EmpVacationMapper empVacationMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    SysEventMapper sysEventMapper;

    @Override
    public boolean insertVacation(Vacation vacation) {
        vacationMapper.insert(vacation);
        return true;
    }

    /**
     * 获取所有假期列表
     * @return List<Vacation>
     */
    @Override
    public List<Vacation> vacationList() {
        return vacationMapper.vacationList();
    }

    /**
     * 员工请假
     * @param empVacation
     * @return boolean
     */
    @Override
    public boolean insertEmpVacation(EmpVacation empVacation) {
        empVacationMapper.insert(empVacation);
        return true;
    }

    /**
     * 获取员工申请假期列表(已提交未审核)
     * @param deptId
     * @return List<AuditVacationDTO>
     */
    @Override
    public List<AuditVacationDTO> auditVacationList(Integer deptId) {
        List<AuditVacationDTO> auditVacationDTOList = vacationMapper.auditVacationDTO(deptId);
        for (AuditVacationDTO auditVacationDTO : auditVacationDTOList){
            // 因为返回到前台的数据是一个字符串 表示假期开始时间-结束时间
            String beginTime = DateUtil.format(auditVacationDTO.getBeginTime(),"yyyy/MM/dd");
            String endTime = DateUtil.format(auditVacationDTO.getEndTime(),"yyyy/MM/dd");
            auditVacationDTO.setVacationStr(beginTime+"-"+endTime);
        }
        return auditVacationDTOList;
    }

    /**
     * 修改员工请假信息
     * @param empVacation
     * @return boolean
     */
    @Override
    public boolean modifyEmpVacation(EmpVacation empVacation) {

        //修改申请状态(批准or驳回)
        empVacationMapper.updateEmpVacation(empVacation);
        if(empVacation.getReqStatus().equals("已批准")){
            //减少员工相应可以请假天数
            EmpVacation empVacationSession = empVacationMapper.selectById(empVacation.getVacationReqId());
            Long betweenDay = DateUtil.between(empVacationSession.getBeginTime(),empVacationSession.getEndTime(),DateUnit.DAY);
            Employee employee = employeeMapper.selectByPrimaryKey(empVacation.getEmpId());
            Integer remainVacation = employee.getVacation()-betweenDay.intValue();
            employee.setVacation(remainVacation);
            employeeMapper.updateEmployee(employee);

            // 系统事件备份
            String description = SysEventDescription.addSysEventDescription(employee.getId(),employee.getName(),"员工请假");
            SysEvent sysEvent = SysEventDescription.addSysEvent(DateUtil.date(),description);
            sysEventMapper.insert(sysEvent);
        }
        return true;
    }


    /**
     * 获取所有请假信息
     * @return List<EmpVacation>
     */
    @Override
    public List<EmpVacation> applyVacationList(Integer empId){
        return empVacationMapper.applyVacationList(empId);
    }
}
